package com.ruoyi.common.utils.equipment;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author F
 */
public class Demo {

    public static void main(String[] args) throws ParseException {
        Calendar calendar=Calendar.getInstance();
//        String TimeNow = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTimeInMillis());
//        System.out.println(TimeNow);
//        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
//        Date date =sdf.parse(TimeNow);
//        Calendar calendar1 = Calendar.getInstance();
//        calendar1.setTime(date);
//        System.out.println(calendar1);
        SimpleDateFormat  mFormat = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
        String date1 ="2012-05-02";
        String aa ="10:30";
        Date date = mFormat.parse(date1+":"+aa+":00");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);


        calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+1);

        System.out.println(mFormat.format(calendar.getTimeInMillis()) );
        System.out.println(mFormat.format(cal.getTimeInMillis()) );
        getQrCode(calendar,12548);
    }

    public static String getQrCode(Calendar calendar, int cardNum) {
        int year = calendar.get(Calendar.YEAR) - 2018;
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
       String Tbit2= toBinary(second,6)+toBinary(minute,6)+toBinary(hour,5)+toBinary(day,5)+toBinary(month,4)+toBinary(year,6);
       long iTime=Long.valueOf(Tbit2,2);
       String sTime= Long.toHexString(iTime);
        byte[] bTime= HEXUtil.HexToByte(sTime,4);
        System.out.println(HEXUtil.ByteToHex(bTime));
        String sCard= Integer.toHexString(cardNum);
        byte [] bCard= HEXUtil.HexToByte(sCard,9);
        byte [] bnew=new byte[bTime.length+bCard.length];
        System.arraycopy(bCard,0,bnew,0,bCard.length);
        System.arraycopy(bTime,0,bnew,bCard.length,bTime.length);
        int crc8=   HEXUtil.Get_CRC8(bnew,bnew.length);
        byte[] bRC4Key= "1e30b3ec0f634874956e27627dfc2c46".getBytes();
        byte [] data= RC4.encry_decry_RC4(bnew,bRC4Key);
        byte [] bytes= new byte[data.length+2];
        bytes[0]=46;
        System.arraycopy(data,0,bytes,1,data.length);
        bytes[bytes.length-1]=(byte)crc8;

    System.out.println(HEXUtil.ByteToHex(bytes));
    return HEXUtil.ByteToHex(bytes);
    }
    public static byte[] reserve( byte[] arr ){
        byte[] arr1 = new byte[arr.length];
        for( int x=0;x<arr.length;x++ ){
            arr1[x] = arr[arr.length-x-1];
        }
        return arr1 ;
    }
    /**
     * 将一个int数字转换为二进制的字符串形式。
     * @param num 需要转换的int类型数据
     * @param digits 要转换的二进制位数，位数不足则在前面补0
     * @return 二进制的字符串形式
     */
    public static String toBinary(int num, int digits) {
        int value = 1 << digits | num;
        //0x20 | 这个是为了保证这个string长度是6位数
        String bs = Integer.toBinaryString(value);
        return  bs.substring(1);
    }

}
